<template>
  <div>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="型号搜索：">
        <el-input v-model="message" placeholder="请输入">
          <template #suffix>
            <i
              class="el-icon-circle-close"
              v-if="message"
              @click="message = ''"
            ></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="onSearch"
          icon="el-icon-search"
          size="small"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div class="bottom">
      <div class="btns">
        <el-button
          icon="el-icon-circle-plus-outline"
          size="small"
          class="create"
          @click="addType"
        >
          新建
        </el-button>
      </div>
      <el-table :data="typeList" highlight-current-row>
        <el-table-column
          type="index"
          label="序号"
          width="80"
          :index="getIndex"
        ></el-table-column>
        <el-table-column prop="name" label="型号名称"></el-table-column>
        <el-table-column prop="model" label="型号编码"></el-table-column>
        <el-table-column label="设备图片">
          <template #default="scope">
            <img :src="scope.row.image" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="vmRow" label="货道行"></el-table-column>
        <el-table-column prop="vmCol" label="货道列"></el-table-column>
        <el-table-column
          prop="channelMaxCapacity"
          label="设备容量"
        ></el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button
              type="text"
              @click="showEdit(scope.row.typeId)"
              class="edit"
              >修改</el-button
            >
            <el-button type="text" plain @click="del" class="del"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="footer" v-if="totalCount > 10 && flag">
        <p>
          <span>共{{ totalCount }}条记录</span>
          <i>第{{ params.pageIndex }}/{{ totalPage }}页</i>
        </p>
        <div class="footer-btns">
          <el-button
            size="mini"
            @click="prePage()"
            :disabled="params.pageIndex === 1"
            :class="{ disbale: params.pageIndex == 1 }"
            >上一页</el-button
          >
          <el-button
            size="mini"
            @click="nextPage()"
            :disabled="params.pageIndex == totalPage"
            :class="{ disbale: params.pageIndex == totalPage }"
            >下一页</el-button
          >
        </div>
      </div>
    </div>
    <!--对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      custom-class="add-dialog"
      @close="closeFn"
    >
      <el-form
        :model="addForm"
        label-width="140px"
        class="add-form"
        :rules="rules"
        ref="typeform"
      >
        <el-form-item label="型号名称：" prop="name">
          <el-input
            type="text"
            placeholder="请输入"
            v-model="addForm.name"
            maxlength="10"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item label="型号编码：" prop="model">
          <el-input
            type="text"
            placeholder="请输入 (限制数字、字母、中划线、下划线)"
            v-model="addForm.model"
            maxlength="15"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item label="货道行数：" prop="vmRow">
          <el-input-number
            v-model="addForm.vmRow"
            controls-position="right"
            :min="1"
            :max="10"
            placeholder="请输入"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="货道列数：" prop="vmCol">
          <el-input-number
            v-model="addForm.vmCol"
            controls-position="right"
            :min="1"
            :max="10"
            placeholder="请输入"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="货道容量：" prop="channelMaxCapacity">
          <el-input-number
            v-model="addForm.channelMaxCapacity"
            controls-position="right"
            :min="1"
            :max="10"
            placeholder="请输入"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="设备图片：" prop="image">
          <el-upload
            :headers="{ Accept: 'application/json,text/plain, */*' }"
            name="fileName"
            class="avatar-uploader"
            action="http://likede2-admin.itheima.net/likede/api/vm-service/sku/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="addForm.image" :src="addForm.image" class="avatar" />
            <i v-else class="el-icon-upload2 avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              支持扩展名：jpg、png，文件不得大于100kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTypeConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getTypeList,
  getAddType,
  getTypeDetail,
  editTypeDetail
} from '@/api/device'
export default {
  name: 'typemanage-page',
  components: {},
  props: {},
  data() {
    const validateModel = (rule, value, callback) => {
      const reg = /^[A-Za-z0-9_-]{1,}$/
      if (reg.test(value)) {
        callback()
      } else {
        callback(new Error('请输入合法的型号编码'))
      }
    }
    return {
      message: '',
      typeList: [],
      params: {
        pageIndex: 1,
        pageSize: 10,
        name: ''
      },
      totalCount: null,
      totalPage: null,
      flag: true,
      dialogFormVisible: false,
      text: '',
      title: null,
      addForm: {
        name: '',
        model: '',
        vmRow: undefined,
        vmCol: undefined,
        channelMaxCapacity: undefined,
        image: ''
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        model: [
          { required: true, message: '请输入', trigger: 'blur' },
          { validator: validateModel, trigger: 'blur' }
        ],
        vmRow: [{ required: true, message: '请输入', trigger: 'blur' }],
        vmCol: [{ required: true, message: '请输入', trigger: 'blur' }],
        channelMaxCapacity: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        image: [{ required: true, message: '请上传', trigger: 'blur' }]
      },
      imageUrl: ''
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getTypeList()
  },
  methods: {
    async getTypeList() {
      const res = await getTypeList(this.params)
      console.log(res)
      this.typeList = res.currentPageRecords
      this.totalCount = res.totalCount
      this.totalPage = res.totalPage
    },
    getIndex(index) {
      return (this.params.pageIndex - 1) * this.params.pageSize + index + 1
    },
    prePage() {
      this.params.pageIndex--
      this.getTypeList()
    },
    nextPage() {
      this.params.pageIndex++
      this.getTypeList()
    },
    async onSearch() {
      if (this.message === '') {
        this.flag = true
        this.params.name = ''
        return this.getTypeList()
      } else {
        this.flag = false
        this.params.name = this.message
        const { data: res } = await getTypeList(this.params)
        console.log(res)
        this.typeList = res.currentPageRecords
      }
    },
    del() {
      this.$notify({
        title: '消息',
        message: '演示系统，不支持此操作',
        type: 'warning'
      })
    },
    handleAvatarSuccess(res, file) {
      // console.log(res)
      // console.log(file)
      // this.imageUrl = URL.createObjectURL(file.raw)
      this.addForm.image = res
    },
    beforeAvatarUpload(file) {
      console.log(file)
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt100KB = (file.size / 1024 / 1024 / 1024) * 100 <= 100

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
      }
      if (!isLt100KB) {
        this.$message.error('上传头像图片大小不能超过 100KB!')
      }
      return isJPG && isLt100KB
    },
    addType() {
      this.title = '新增设备类型'
      this.addForm = {
        name: '',
        model: '',
        vmRow: undefined,
        vmCol: undefined,
        channelMaxCapacity: undefined,
        image: ''
      }
      // this.$nextTick(() => {
      //   this.$refs.typeform.resetFields()
      // })
      // console.log(this.$refs)
      this.dialogFormVisible = true
    },
    closeFn() {
      if (this.title === '新增设备类型') {
        this.$refs.typeform.resetFields()
      }
    },
    async addTypeConfirm() {
      try {
        await this.$refs.typeform.validate()
        console.log(11111)
        if (this.title === '新增设备类型') {
          // 添加
          const params = {
            ...this.addForm
          }
          await getAddType(params)
          this.$message.success('添加成功')
        } else {
          // 编辑
          console.log(this.addForm.typeId)
          await editTypeDetail(this.addForm.typeId, this.addForm)
          this.$message.success('修改成功')
        }
        this.getTypeList()
        this.dialogFormVisible = false
      } catch (error) {
        console.log(error)
        if (error.response?.data) {
          this.$message.error(error.response.data)
        }
      }
    },
    async showEdit(id) {
      this.title = '修改设备类型'
      const res = await getTypeDetail(id)
      // console.log(res)
      this.addForm = res
      // console.log(this.addForm)
      this.dialogFormVisible = true
    }
  }
}
</script>

<style scoped lang="scss">
.demo-form-inline {
  background-color: #fff;
  height: 64px;
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  .el-form-item {
    padding-left: 15px;
    margin-top: 22px;
    ::v-deep .el-input__inner {
      width: 206px;
      padding: 0 30px 0 15px;
      height: 36px;
    }
  }
  .el-form-item:nth-child(2) {
    padding-left: 0;
  }
  .el-button {
    height: 36px;
    width: 80px;
    border-radius: 5px;
  }
}
.bottom {
  padding: 20px 15px 19px 17px;
  background-color: #fff;
  ::v-deep .el-table .has-gutter tr th {
    background-color: #f3f6fb !important;
    border-bottom: 0 !important;
    padding: 10px 0;
    color: #666;
    font-weight: normal;
  }
  ::v-deep .el-table .has-gutter tr th:first-child {
    border-radius: 5px 0 0 5px;
  }
  ::v-deep .el-table .has-gutter tr th:nth-child(8) {
    border-radius: 0 5px 5px 0;
  }
  ::v-deep .el-table__cell {
    padding: 3px 0;
    border-bottom: 0;
    img {
      width: 24px;
      height: 24px;
      border-radius: 50%;
      background: #f3f6fb;
      border: 1px solid #f3f6fb;
    }
    .edit {
      color: #5f84ff;
    }
    .del {
      color: #ff5a5a;
    }
  }
  .btns {
    margin-bottom: 20px;
    .el-button {
      font-size: 14px;
      height: 36px;
      border: 0;
      border-radius: 5px;
    }
    .create {
      background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
      color: #fff;
    }
  }
  .footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 32px 16px;
    p {
      font-size: 16px;
      line-height: 28px;
      color: #dbdfe5;
    }
    .el-button {
      margin: 0 10px;
      background-color: #d5ddf8;
      height: 32px;
      color: #606266;
    }
  }
  .disbale {
    background-color: #edf0f9 !important;
    color: #d8dde3 !important;
  }
}
.el-icon-circle-close {
  margin-right: 5px;
  color: #dfe4e7;
}
.el-icon-circle-close:hover {
  color: #7f7270;
  cursor: pointer;
}
::v-deep .el-notification .el-icon-info {
  color: #ff7c0f;
}
//头像上传
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background-color: #f3f6fb;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 18px;
  color: #bac0cd;
  width: 84px;
  height: 84px;
  line-height: 84px;
  text-align: center;
}
.avatar {
  width: 84px;
  height: 84px;
  display: block;
}
::v-deep .el-upload__tip {
  font-size: 14px;
  color: #bac0cd;
  margin-top: 7px;
  line-height: 1.15;
}
// 弹出框
::v-deep .add-dialog {
  width: 630px;
  border-radius: 10px;
  font-size: 14px;
  color: #666;
  .el-dialog__title {
    line-height: 22px;
    font-size: 16px;
    font-weight: 600;
  }
  .el-dialog__body {
    height: 462px;
    padding: 20px 20px 30px;
    .add-form {
      .el-form-item__label,
      .el-form-item__content {
        line-height: 36px;
      }
      .el-input__inner {
        height: 36px;
      }
      .el-input {
        width: 396px;
        .el-input__count {
          color: #bac0cd;
        }
      }
      .el-input-number {
        width: 396px;
        line-height: 36px;
      }
    }
  }
  .el-dialog__footer {
    padding: 0 20px 40px;
    text-align: center;
    .el-button + .el-button {
      margin-left: 34px;
    }
    .el-button {
      width: 80px;
      height: 36px;
      border-radius: 5px;
      border: 0;
      padding: 0;
      &:nth-child(1) {
        background-color: #fbf4f0;
      }
      &:nth-child(2) {
        background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
      }
    }
  }
}
</style>
